package com.zzyl.nursing.service.impl;

import java.util.List;

import com.zzyl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-05
 */
@Service
public class NursingLevelServiceImpl implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final String CACHE_KEY = "nursingLevel:all";
    private static final String CACHE_KEY_LIST = "nursingLevel:list";

    /**
     * id查询单个护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Integer id) {
        return nursingLevelMapper.selectNursingLevelById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        // 从缓存中获取
//        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get(CACHE_KEY_LIST);
//        if (list != null) {
//            return list;
//        }
        // 缓存中没有.从数据库中获取
        List<NursingLevel> nursingLevelList = nursingLevelMapper.selectNursingLevelList(nursingLevel);
        // 缓存数据
        //redisTemplate.opsForValue().set(CACHE_KEY_LIST, nursingLevelList);
        return nursingLevelList;
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        int flag = nursingLevelMapper.insertNursingLevel(nursingLevel);
        if (flag > 0) {
            // 删除缓存
            deleteCache();
        }
        return flag;
    }

    /**
     * 删除缓存
     */
    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY);
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        nursingLevel.setUpdateTime(DateUtils.getNowDate());
        int flag = nursingLevelMapper.updateNursingLevel(nursingLevel);
        if (flag > 0) {
            // 删除缓存
            deleteCache();
        }
        return flag;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Integer[] ids) {
        int flag = nursingLevelMapper.deleteNursingLevelByIds(ids);
        if (flag > 0) {
            // 删除缓存
            deleteCache();
        }
        return flag;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Integer id) {
        int flag = nursingLevelMapper.deleteNursingLevelById(id);
        if (flag > 0) {
            // 删除缓存
            deleteCache();
        }
        return flag;
    }

    /**
     * 查询所有护理等级
     *
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> nursingLevelAll() {
        // 从缓存中获取
        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get(CACHE_KEY);
        if (list != null) {
            return list;
        }
        // 缓存中没有.从数据库中获取
        List<NursingLevel> nursingLevels = nursingLevelMapper.listAll();
        // 缓存数据
        redisTemplate.opsForValue().set(CACHE_KEY, nursingLevels);
        return nursingLevels;
    }
}
